<?php

/**
 * @author lorenzo
 */

    require_once(dirname(__FILE__)."/../config.php");

class DBmanager {

    private $mysql;
    private static $instance;

    private function  __construct() {
        $this->connect();
    }

    public static function getInstance() {
        if (!self::$instance) {
            self::$instance = new DBmanager();
        }

        return self::$instance;
    }

    public function connect() {
        global $usr, $passwd, $dbname;
        
        $this->mysql = new mysqli('localhost', $usr, $passwd, $dbname);
    }

    public function disconnect() {
        $this->mysql->close();
    }

    public function query($query) {
        $this->connect();
        return $this->mysql->query($query);
    }

    public function check_login($usr, $passwd) {
        $query="SELECT * FROM user WHERE usr='".$usr."' AND passwd=MD5('".$passwd."')";
        $res=$this->query($query);
        if ($res ) {
            $obj=$res->fetch_object();
            return  $obj;
        }

        return false;
    }

    public function get_node($nid, $preserve=false) {
        $query="SELECT id, title, description, author, inserted, published, content_type FROM core WHERE id=".intval($nid);
        $res=$this->query($query);
        $row1=$res->fetch_assoc();
        if (!empty($row1['content_type'])) {
            $query="SELECT * FROM content_type_".$row1['content_type']." WHERE nid=".$nid;
            $res=$this->query($query);
            $row2=$res->fetch_assoc();
            unset($row2['nid']);
            $row1=array_merge($row1, $row2);
        }
        if (!$preserve)
            unset($row1['id'], $row1['content_type']);

        return $row1;
    }
}

?>